
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>8.3 需求5: 广告黑名单实时统计 · 大数据 bigdata_mallanalyze_atguigu 课堂笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content=" 李振超">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-page-toc-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-prism/prism-solarizedlight.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-change_girls/girls.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../../styles/website.css">
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../di-9-zhang-xu-qiu-6-guang-gao-dian-ji-liang-shi-shi-tong-ji.html" />
    
    
    <link rel="prev" href="81-shi-shi-huan-jing-da-jian.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.atguigu.com/" target="_blank" class="custom-link">联系我们</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        <li class="header">第 1 部分 电商分析系统介绍</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    第 1 章 项目简介
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../../11-xiang-mu-zheng-ti-jie-shao.html">
            
                <a href="../../11-xiang-mu-zheng-ti-jie-shao.html">
            
                    
                    1.1 项目整体介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="../../12-xiang-mu-zheng-ti-jia-gou.html">
            
                <a href="../../12-xiang-mu-zheng-ti-jia-gou.html">
            
                    
                    1.2 项目整体架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="../../13-ye-wu-xu-qiu.html">
            
                <a href="../../13-ye-wu-xu-qiu.html">
            
                    
                    1.3 数据源分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="../../14-xiang-mu-xu-qiu.html">
            
                <a href="../../14-xiang-mu-xu-qiu.html">
            
                    
                    1.4 项目需求
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../chapter1.html">
            
                <a href="../../chapter1.html">
            
                    
                    第 2 章 预备知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../di-3-zhang-xiang-mu-da-jian.html">
            
                <a href="../../di-3-zhang-xiang-mu-da-jian.html">
            
                    
                    第 3 章 项目搭建
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../di-3-zhang-xiang-mu-da-jian/31-chuang-jian-fu-gong-cheng.html">
            
                <a href="../../di-3-zhang-xiang-mu-da-jian/31-chuang-jian-fu-gong-cheng.html">
            
                    
                    3.1 创建父工程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../../di-3-zhang-xiang-mu-da-jian/32-chuang-jian-gong-zuo-zi-mo-kuai.html">
            
                <a href="../../di-3-zhang-xiang-mu-da-jian/32-chuang-jian-gong-zuo-zi-mo-kuai.html">
            
                    
                    3.2 创建通用子模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../../di-3-zhang-xiang-mu-da-jian/33-chuang-jian-mo-ni-shu-ju-zi-mo-kuai.html">
            
                <a href="../../di-3-zhang-xiang-mu-da-jian/33-chuang-jian-mo-ni-shu-ju-zi-mo-kuai.html">
            
                    
                    3.3 创建模拟数据子模块
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">第 2 部分 电商分析系统需求实现</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../di-4-zhang-xu-qiu-1.html">
            
                <a href="../di-4-zhang-xu-qiu-1.html">
            
                    
                    第 4 章 需求1: Top10 热门品类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../di-5-zhang-xu-qiu-2-top10-remen-pin-lei-zhong-de-top10-session-tong-ji.html">
            
                <a href="../di-5-zhang-xu-qiu-2-top10-remen-pin-lei-zhong-de-top10-session-tong-ji.html">
            
                    
                    第 5 章 需求2: Top10热门品类中的 Top10 活跃 Session 统计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../di-6-zhang-xu-qiu-3-dan-ye-mian-tiao-zhuan-lv.html">
            
                <a href="../di-6-zhang-xu-qiu-3-dan-ye-mian-tiao-zhuan-lv.html">
            
                    
                    第 6 章 需求3: 页面单条转化率统计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../di-7-zhang-xu-qiu-4-ge-qu-yu-re-men-shang-pin-top3.html">
            
                <a href="../di-7-zhang-xu-qiu-4-ge-qu-yu-re-men-shang-pin-top3.html">
            
                    
                    第 7 章 需求4: 各区域热门商品 Top3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../di-8-zhang-guang-gao-dian-ji-liang-shi-shi-tong-ji.html">
            
                <a href="../di-8-zhang-guang-gao-dian-ji-liang-shi-shi-tong-ji.html">
            
                    
                    第 8 章 需求5:  广告黑名单实时统计
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.1" data-path="81-li-xian-xiang-mu-yu-bei-zhi-shi.html">
            
                <a href="81-li-xian-xiang-mu-yu-bei-zhi-shi.html">
            
                    
                    8.1 离线项目预备知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.2" data-path="81-shi-shi-huan-jing-da-jian.html">
            
                <a href="81-shi-shi-huan-jing-da-jian.html">
            
                    
                    8.2 实时环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.5.3" data-path="82-xu-qiu-5-guang-gao-hei-ming-dan-shi-shi-tong-ji.html">
            
                <a href="82-xu-qiu-5-guang-gao-hei-ming-dan-shi-shi-tong-ji.html">
            
                    
                    8.3 需求5: 广告黑名单实时统计
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../di-9-zhang-xu-qiu-6-guang-gao-dian-ji-liang-shi-shi-tong-ji.html">
            
                <a href="../di-9-zhang-xu-qiu-6-guang-gao-dian-ji-liang-shi-shi-tong-ji.html">
            
                    
                    第 9 章 需求6: 广告点击量实时统计 
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../di-10-zhang-xu-qiu-7-mei-tian-mei-di-qu-re-men-guang-gao-top3.html">
            
                <a href="../di-10-zhang-xu-qiu-7-mei-tian-mei-di-qu-re-men-guang-gao-top3.html">
            
                    
                    第 10 章 需求7: 每天每地区 热门广告 Top3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../di-11-zhang-xin-zeng-xu-6c423a-zui-jin-1-xiao-shi-guang-gao-dian-ji-liang-shi-shi-tong-ji.html">
            
                <a href="../di-11-zhang-xin-zeng-xu-6c423a-zui-jin-1-xiao-shi-guang-gao-dian-ji-liang-shi-shi-tong-ji.html">
            
                    
                    第 11 章 新增需求: 最近 1 小时广告点击量实时统计
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >8.3 需求5: 广告黑名单实时统计</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="82-&#x9700;&#x6C42;5-&#x5E7F;&#x544A;&#x9ED1;&#x540D;&#x5355;&#x5B9E;&#x65F6;&#x7EDF;&#x8BA1;">8.2 &#x9700;&#x6C42;5: &#x5E7F;&#x544A;&#x9ED1;&#x540D;&#x5355;&#x5B9E;&#x65F6;&#x7EDF;&#x8BA1;</h1>
<h2 id="821-&#x9700;&#x6C42;&#x7B80;&#x4ECB;">8.2.1 &#x9700;&#x6C42;&#x7B80;&#x4ECB;</h2>
<p>&#x5B9E;&#x73B0;&#x5B9E;&#x65F6;&#x7684;&#x52A8;&#x6001;&#x9ED1;&#x540D;&#x5355;&#x673A;&#x5236;&#xFF1A;&#x5C06;&#x6BCF;&#x5929;&#x5BF9;&#x67D0;&#x4E2A;&#x5E7F;&#x544A;&#x70B9;&#x51FB;&#x8D85;&#x8FC7; 100 &#x6B21;&#x7684;&#x7528;&#x6237;&#x62C9;&#x5165;&#x9ED1;&#x540D;&#x5355;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;:</p>
</blockquote>
<ol>
<li><p>&#x6211;&#x4EEC;&#x628A;&#x9ED1;&#x540D;&#x5355;&#x4FDD;&#x5B58;&#x5728; redis &#x4E2D;</p>
</li>
<li><p>&#x5BF9;&#x5DF2;&#x7ECF;&#x8FDB;&#x5165;&#x9ED1;&#x540D;&#x5355;&#x7684;&#x7528;&#x6237;&#x5C06;&#x4E0D;&#x4F1A;&#x518D;&#x8FDB;&#x884C;&#x68C0;&#x67E5;.</p>
</li>
</ol>
<h2 id="822-&#x601D;&#x8DEF;">8.2.2 &#x601D;&#x8DEF;</h2>
<ol>
<li><p>&#x4F7F;&#x7528;&#x9ED1;&#x540D;&#x5355;&#x8FC7;&#x6EE4; DStream, &#x5F97;&#x5230;&#x65B0;&#x7684;&#x4E0D;&#x5305;&#x542B;&#x9ED1;&#x540D;&#x5355;&#x7684;&#x7528;&#x6237;&#x7684;&#x70B9;&#x51FB;&#x65E5;&#x5FD7;&#x8BB0;&#x5F55;&#x7684;&#x65B0;&#x7684; DStream</p>
</li>
<li><p>&#x65B0;&#x7684;&#x8FC7;&#x6EE4;&#x540E;&#x7684; DStream &#x9700;&#x8981;&#x5B9E;&#x65F6;&#x76D1;&#x6D4B;&#x662F;&#x5426;&#x6709;&#x65B0;&#x7684;&#x7528;&#x6237;&#x9700;&#x8981;&#x52A0;&#x5165;&#x5230;&#x9ED1;&#x540D;&#x5355;. </p>
<ul>
<li><p>&#x5728; redis&#x4E2D;&#x4E3A;&#x6BCF;&#x4E2A;<code>userid_day_adsclick</code>&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x8BA1;&#x6570;&#x5668;(&#x4F7F;&#x7528; hash &#x6765;&#x5B58;&#x50A8;&#x6570;&#x636E;)</p>
</li>
<li><p>&#x5F53;&#x8BA1;&#x6570;&#x5668;&#x7684;&#x503C;&#x8D85;&#x8FC7;&#x9884;&#x5B9A;&#x7684;&#x9608;&#x503C;&#x7684;&#x65F6;&#x5019;, &#x52A0;&#x5165;&#x9ED1;&#x540D;&#x5355;(&#x4F7F;&#x7528; set &#x96C6;&#x5408;&#x6765;&#x5B58;&#x50A8;&#x9ED1;&#x540D;&#x5355;, &#x6BCF;&#x4E2A;&#x5143;&#x7D20;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x7528;&#x6237; id)</p>
</li>
</ul>
</li>
</ol>
<h2 id="823-&#x5177;&#x4F53;&#x7684;&#x4E1A;&#x52A1;&#x5B9E;&#x73B0;">8.2.3 &#x5177;&#x4F53;&#x7684;&#x4E1A;&#x52A1;&#x5B9E;&#x73B0;</h2>
<h3 id="&#x6BCF;&#x4E2A;&#x5E7F;&#x544A;&#x70B9;&#x51FB;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;">&#x6BCF;&#x4E2A;&#x5E7F;&#x544A;&#x70B9;&#x51FB;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;</h3>
<pre class="language-"><code class="lang-scala"><span class="token keyword">package</span> com<span class="token punctuation">.</span>atgugu<span class="token punctuation">.</span>sparkmall<span class="token punctuation">.</span>realtime<span class="token punctuation">.</span>bean

<span class="token keyword">import</span> java<span class="token punctuation">.</span>text<span class="token punctuation">.</span>SimpleDateFormat
<span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Date

<span class="token keyword">case</span> <span class="token keyword">class</span> AdsInfo<span class="token punctuation">(</span>ts<span class="token operator">:</span> <span class="token builtin">Long</span><span class="token punctuation">,</span> area<span class="token operator">:</span> <span class="token builtin">String</span><span class="token punctuation">,</span> city<span class="token operator">:</span> <span class="token builtin">String</span><span class="token punctuation">,</span> userId<span class="token operator">:</span> <span class="token builtin">String</span><span class="token punctuation">,</span> adsId<span class="token operator">:</span> <span class="token builtin">String</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">val</span> dayString<span class="token operator">:</span> <span class="token builtin">String</span> <span class="token operator">=</span> <span class="token keyword">new</span> SimpleDateFormat<span class="token punctuation">(</span><span class="token string">&quot;yyyy-MM-dd&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>format<span class="token punctuation">(</span><span class="token keyword">new</span> Date<span class="token punctuation">(</span>ts<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">override</span> <span class="token keyword">def</span> toString<span class="token operator">:</span> <span class="token builtin">String</span> <span class="token operator">=</span> s<span class="token string">&quot;$dayString:$area:$city:$adsId&quot;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="&#x5177;&#x4F53;&#x7684;&#x5B9E;&#x73B0;">&#x5177;&#x4F53;&#x7684;&#x5B9E;&#x73B0;</h3>
<pre class="language-"><code class="lang-scala"><span class="token keyword">package</span> com<span class="token punctuation">.</span>atgugu<span class="token punctuation">.</span>sparkmall<span class="token punctuation">.</span>realtime<span class="token punctuation">.</span>app

<span class="token keyword">import</span> java<span class="token punctuation">.</span>util

<span class="token keyword">import</span> com<span class="token punctuation">.</span>atgugu<span class="token punctuation">.</span>sparkmall<span class="token punctuation">.</span>realtime<span class="token punctuation">.</span>bean<span class="token punctuation">.</span>AdsInfo
<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>spark<span class="token punctuation">.</span>SparkContext
<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>spark<span class="token punctuation">.</span>broadcast<span class="token punctuation">.</span>Broadcast
<span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>spark<span class="token punctuation">.</span>streaming<span class="token punctuation">.</span>dstream<span class="token punctuation">.</span>DStream
<span class="token keyword">import</span> redis<span class="token punctuation">.</span>clients<span class="token punctuation">.</span>jedis<span class="token punctuation">.</span>Jedis

<span class="token keyword">object</span> BlackListApp <span class="token punctuation">{</span>
    <span class="token comment">// redis&#x7684;&#x4E00;&#x4E9B;&#x76F8;&#x5173;&#x53C2;&#x6570;</span>
    <span class="token keyword">val</span> redisIp <span class="token operator">=</span> <span class="token string">&quot;hadoop201&quot;</span>
    <span class="token keyword">val</span> redisPort <span class="token operator">=</span> <span class="token number">6379</span>
    <span class="token keyword">val</span> countKey <span class="token operator">=</span> <span class="token string">&quot;user:day:adsclick&quot;</span>
    <span class="token keyword">val</span> blackListKey <span class="token operator">=</span> <span class="token string">&quot;blacklist&quot;</span>

    <span class="token comment">// &#x8FC7;&#x6EE4;&#x9ED1;&#x540D;&#x5355;&#x4E2D;&#x7684;&#x7528;&#x6237;. &#x8FD4;&#x56DE;&#x503C;&#x662F;&#x4E0D;&#x5305;&#x542B;&#x9ED1;&#x540D;&#x5355;&#x7684;&#x7528;&#x6237;&#x7684;&#x5E7F;&#x544A;&#x70B9;&#x51FB;&#x8BB0;&#x5F55;&#x7684; DStream</span>
    <span class="token keyword">def</span> checkUserFromBlackList<span class="token punctuation">(</span>adsClickInfoDStream<span class="token operator">:</span> DStream<span class="token punctuation">[</span>AdsInfo<span class="token punctuation">]</span><span class="token punctuation">,</span> sc<span class="token operator">:</span> SparkContext<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
        adsClickInfoDStream<span class="token punctuation">.</span>transform<span class="token punctuation">(</span>rdd <span class="token keyword">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">val</span> jedis <span class="token operator">=</span> <span class="token keyword">new</span> Jedis<span class="token punctuation">(</span>redisIp<span class="token punctuation">,</span> redisPort<span class="token punctuation">)</span>
            <span class="token comment">// &#x8BFB;&#x51FA;&#x6765;&#x9ED1;&#x540D;&#x5355;</span>
            <span class="token keyword">val</span> blackList<span class="token operator">:</span> util<span class="token punctuation">.</span>Set<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span> <span class="token operator">=</span> jedis<span class="token punctuation">.</span>smembers<span class="token punctuation">(</span>blackListKey<span class="token punctuation">)</span>
            <span class="token keyword">val</span> blackListBC<span class="token operator">:</span> Broadcast<span class="token punctuation">[</span>util<span class="token punctuation">.</span>Set<span class="token punctuation">[</span><span class="token builtin">String</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> sc<span class="token punctuation">.</span>broadcast<span class="token punctuation">(</span>blackList<span class="token punctuation">)</span>
            jedis<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
            rdd<span class="token punctuation">.</span>filter<span class="token punctuation">(</span>adsInfo <span class="token keyword">=&gt;</span> <span class="token punctuation">{</span>
                <span class="token operator">!</span>blackListBC<span class="token punctuation">.</span>value<span class="token punctuation">.</span>contains<span class="token punctuation">(</span>adsInfo<span class="token punctuation">.</span>userId<span class="token punctuation">)</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>


    <span class="token comment">// &#x628A;&#x7528;&#x6237;&#x5199;&#x5165;&#x5230;&#x9ED1;&#x540D;&#x5355;&#x4E2D;</span>
    <span class="token keyword">def</span> checkUserToBlackList<span class="token punctuation">(</span>adsInfoDStream<span class="token operator">:</span> DStream<span class="token punctuation">[</span>AdsInfo<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
        adsInfoDStream<span class="token punctuation">.</span>foreachRDD<span class="token punctuation">(</span>rdd <span class="token keyword">=&gt;</span> <span class="token punctuation">{</span>
            rdd<span class="token punctuation">.</span>foreachPartition<span class="token punctuation">(</span>adsInfoIt <span class="token keyword">=&gt;</span> <span class="token punctuation">{</span>
                <span class="token keyword">val</span> jedis <span class="token operator">=</span> <span class="token keyword">new</span> Jedis<span class="token punctuation">(</span>redisIp<span class="token punctuation">,</span> redisPort<span class="token punctuation">)</span>
                <span class="token comment">// &#x5EFA;&#x7ACB;redis &#x8FDE;&#x63A5;</span>
                adsInfoIt<span class="token punctuation">.</span>foreach<span class="token punctuation">(</span>adsInfo <span class="token keyword">=&gt;</span> <span class="token punctuation">{</span>
                    <span class="token comment">//1. &#x5728; redis &#x4E2D;&#x8BA1;&#x6570;. &#x4F7F;&#x7528; set Key: user:day:adsId   field: ...</span>
                    <span class="token keyword">val</span> countField <span class="token operator">=</span>
                        s<span class="token string">&quot;${adsInfo.userId}:${adsInfo.dayString}:${adsInfo.adsId}&quot;</span>
                    jedis<span class="token punctuation">.</span>hincrBy<span class="token punctuation">(</span>countKey<span class="token punctuation">,</span> countField<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &#x8BA1;&#x6570; + 1</span>
                    <span class="token comment">// 2. &#x8FBE;&#x5230;&#x9608;&#x503C;&#x540E;&#x5199;&#x5165;&#x9ED1;&#x540D;&#x5355;</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>jedis<span class="token punctuation">.</span>hget<span class="token punctuation">(</span>countKey<span class="token punctuation">,</span> countField<span class="token punctuation">)</span><span class="token punctuation">.</span>toLong <span class="token operator">&gt;=</span> <span class="token number">100</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// &#x5982;&#x679C;&#x70B9;&#x51FB;&#x67D0;&#x4E2A;&#x5E7F;&#x544A;&#x7684;&#x6570;&#x91CF;&#x8D85;&#x8FC7; 100</span>
                        jedis<span class="token punctuation">.</span>sadd<span class="token punctuation">(</span>blackListKey<span class="token punctuation">,</span> adsInfo<span class="token punctuation">.</span>userId<span class="token punctuation">)</span> <span class="token comment">// &#x52A0;&#x5165;&#x9ED1;&#x540D;&#x5355;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span><span class="token punctuation">)</span>
                jedis<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="&#x4F7F;&#x7528;&#x8FDE;&#x63A5;&#x6C60;">&#x4F7F;&#x7528;&#x8FDE;&#x63A5;&#x6C60;</h3>
<p>&#x5728;<code>sparkmall-common</code>&#x6A21;&#x5757;&#x4E2D;&#x521B;&#x5EFA;<code>RedisUtil</code>&#x5DE5;&#x5177;&#x7C7B;</p>
<pre class="language-"><code class="lang-scala"><span class="token keyword">package</span> com<span class="token punctuation">.</span>atguigu<span class="token punctuation">.</span>sparkmall<span class="token punctuation">.</span>common<span class="token punctuation">.</span>util

<span class="token keyword">import</span> redis<span class="token punctuation">.</span>clients<span class="token punctuation">.</span>jedis<span class="token punctuation">.</span><span class="token punctuation">{</span>Jedis<span class="token punctuation">,</span> JedisPool<span class="token punctuation">,</span> JedisPoolConfig<span class="token punctuation">}</span>

<span class="token keyword">object</span> RedisUtil <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">val</span> config <span class="token operator">=</span> ConfigurationUtil<span class="token punctuation">(</span><span class="token string">&quot;config.properties&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token keyword">val</span> host <span class="token operator">=</span> config<span class="token punctuation">.</span>getString<span class="token punctuation">(</span><span class="token string">&quot;redis.host&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token keyword">val</span> port <span class="token operator">=</span> config<span class="token punctuation">.</span>getInt<span class="token punctuation">(</span><span class="token string">&quot;redis.port&quot;</span><span class="token punctuation">)</span>

    <span class="token keyword">private</span> <span class="token keyword">val</span> jedisPoolConfig <span class="token operator">=</span> <span class="token keyword">new</span> JedisPoolConfig<span class="token punctuation">(</span><span class="token punctuation">)</span>
    jedisPoolConfig<span class="token punctuation">.</span>setMaxTotal<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token comment">//&#x6700;&#x5927;&#x8FDE;&#x63A5;&#x6570;</span>
    jedisPoolConfig<span class="token punctuation">.</span>setMaxIdle<span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token comment">//&#x6700;&#x5927;&#x7A7A;&#x95F2;</span>
    jedisPoolConfig<span class="token punctuation">.</span>setMinIdle<span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token comment">//&#x6700;&#x5C0F;&#x7A7A;&#x95F2;</span>
    jedisPoolConfig<span class="token punctuation">.</span>setBlockWhenExhausted<span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token comment">//&#x5FD9;&#x788C;&#x65F6;&#x662F;&#x5426;&#x7B49;&#x5F85;</span>
    jedisPoolConfig<span class="token punctuation">.</span>setMaxWaitMillis<span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span> <span class="token comment">//&#x5FD9;&#x788C;&#x65F6;&#x7B49;&#x5F85;&#x65F6;&#x957F; &#x6BEB;&#x79D2;</span>
    jedisPoolConfig<span class="token punctuation">.</span>setTestOnBorrow<span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token comment">//&#x6BCF;&#x6B21;&#x83B7;&#x5F97;&#x8FDE;&#x63A5;&#x7684;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;</span>
    <span class="token keyword">private</span> <span class="token keyword">val</span> jedisPool<span class="token operator">:</span> JedisPool <span class="token operator">=</span> <span class="token keyword">new</span> JedisPool<span class="token punctuation">(</span>jedisPoolConfig<span class="token punctuation">,</span> host<span class="token punctuation">,</span> port<span class="token punctuation">)</span>

    <span class="token comment">// &#x76F4;&#x63A5;&#x5F97;&#x5230;&#x4E00;&#x4E2A; Redis &#x7684;&#x8FDE;&#x63A5;</span>
    <span class="token keyword">def</span> getJedisClient<span class="token operator">:</span> Jedis <span class="token operator">=</span> <span class="token punctuation">{</span>
        jedisPool<span class="token punctuation">.</span>getResource
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; &#x5C1A;&#x7845;&#x8C37;&#x5927;&#x6570;&#x636E; 2019 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification"><br>&#x8BE5;&#x6587;&#x4EF6;&#x6700;&#x540E;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2019-03-10 17:15:07
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="81-shi-shi-huan-jing-da-jian.html" class="navigation navigation-prev " aria-label="Previous page: 8.2 实时环境搭建">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../di-9-zhang-xu-qiu-6-guang-gao-dian-ji-liang-shi-shi-tong-ji.html" class="navigation navigation-next " aria-label="Next page: 第 9 章 需求6: 广告点击量实时统计 ">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"8.3 需求5: 广告黑名单实时统计","level":"2.5.3","depth":2,"next":{"title":"第 9 章 需求6: 广告点击量实时统计 ","level":"2.6","depth":1,"path":"di-2-bu-fen-dian-shang-fen-xi-xi-tong-xu-qiu-shi-xian/di-9-zhang-xu-qiu-6-guang-gao-dian-ji-liang-shi-shi-tong-ji.md","ref":"di-2-bu-fen-dian-shang-fen-xi-xi-tong-xu-qiu-shi-xian/di-9-zhang-xu-qiu-6-guang-gao-dian-ji-liang-shi-shi-tong-ji.md","articles":[]},"previous":{"title":"8.2 实时环境搭建","level":"2.5.2","depth":2,"path":"di-2-bu-fen-dian-shang-fen-xi-xi-tong-xu-qiu-shi-xian/di-8-zhang-guang-gao-dian-ji-liang-shi-shi-tong-ji/81-shi-shi-huan-jing-da-jian.md","ref":"di-2-bu-fen-dian-shang-fen-xi-xi-tong-xu-qiu-shi-xian/di-8-zhang-guang-gao-dian-ji-liang-shi-shi-tong-ji/81-shi-shi-huan-jing-da-jian.md","articles":[]},"dir":"ltr"},"config":{"plugins":["splitter","expandable-chapters-small","page-toc-button","-highlight","prism","prism-themes","tbfed-pagefooter","sitemap","change_girls","livereload"],"styles":{"website":"styles/website.css","ebook":"styles/ebook.css","pdf":"styles/pdf.css","mobi":"styles/mobi.css","epub":"styles/epub.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy 尚硅谷大数据 2019","modify_label":"<br>该文件最后修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"prism":{"css":["prismjs/themes/prism-solarizedlight.css"]},"disqus":{"shortName":"zhenchao125"},"livereload":{},"splitter":{},"change_girls":{"time":30,"urls":["http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/62171757.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/83394736.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/15906185.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/15906185.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/3394270.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/65028582.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/20359261.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/37472401.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/3355397.jpg-atguiguImg","http://lizhenchao.oss-cn-shenzhen.aliyuncs.com/imgs/18-3-4/11075549.jpg-atguiguImg"]},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"sitemap":{"hostname":"http://bigdataatguigu.applinzi.com/"},"page-toc-button":{"maxTocDepth":3,"minTocSize":2},"prism-themes":{},"expandable-chapters-small":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":" 李振超","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"大数据 bigdata_mallanalyze_atguigu 课堂笔记","language":"zh-hans","links":{"sidebar":{"联系我们":"http://www.atguigu.com/"}},"gitbook":"*","description":"课堂笔记,学生提前预习"},"file":{"path":"di-2-bu-fen-dian-shang-fen-xi-xi-tong-xu-qiu-shi-xian/di-8-zhang-guang-gao-dian-ji-liang-shi-shi-tong-ji/82-xu-qiu-5-guang-gao-hei-ming-dan-shi-shi-tong-ji.md","mtime":"2019-03-10T09:15:07.679Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-19T00:04:29.500Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-page-toc-button/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-change_girls/girls.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

